package com.jlf.core.controller;

import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.jlf.core.annotation.restful.controller.RestfulController;
import com.jlf.core.annotation.restful.method.RestfulPost;
import com.jlf.core.annotation.restful.parameter.RestfulInput;
import com.jlf.core.annotation.restful.parameter.RestfulUsage;
import com.jlf.core.domain._SimpleDO;
import com.jlf.core.server._SimpleServer;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;


/**
 * 简单的接口控制器封装，支持extend和内嵌
 *
 * @author wujr
 * 2023/7/10
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/10 1.0 新增]
 */
@Slf4j
public class _SimpleController<T extends _SimpleDO<T>> {
    protected final _SimpleServer<T> _server;
    protected final Class<T> domainClass;

    @SuppressWarnings("unchecked")
    public _SimpleController(_SimpleServer<T> server){
        this._server = server;
        RestfulController restfulController = this.getClass().getAnnotation(RestfulController.class);
        this.domainClass = (Class<T>)ReflectionKit.getSuperClassGenericType(this.getClass(), _SimpleController.class, 0);
    }

    @RestfulPost(name = "新增", code = "add")
    public <R> R add(@Valid @RequestBody @RestfulInput(RestfulUsage.ADD) Object input) throws Exception{
        T record = this.domainClass.newInstance();

        _server.add(record);
        return (R)record;
    }
}
